const fs = require('fs');
const path = require('path');
const https = require('https');
const url = require('url');
const iconv = require('iconv-lite');
const cheerio = require('cheerio')

// const imgUrl = 'https://wall.alphacoders.com/search.php?search=saber&lang=Chinesetp://placehold.it/300x600/E97452/fff'


const num = 100

const pageNum = num / 30 + 1

var imgJsonData = []

for(let i=0; i<pageNum; i++){
    var imgUrl = 'https://wall.alphacoders.com/search.php?search=saber&page=' + pageNum
    crawler(imgUrl)
}

function crawler(imgUrl) {
    var imgData = []

    function download(imgUrl) {
        var parsedUrl = url.parse(imgUrl)
        var options = {
            method: 'GET',
            hostname: parsedUrl.hostname,
            path: parsedUrl.path
        }
        // console.log(options)
        return new Promise((resolve, reject)=>{
            var request = https.request(options ,function(respond){
                var chunks = []
                respond.on('data', function(chunk){
                    chunks.push(chunk)
                })
                respond.on('error', (e)=>{
                    console.log(e)
                    reject(e)
                })
                respond.on('end', function(){
                    resolve(Buffer.concat(chunks))
                })
            })
            request.end()
        })
    }

    download(imgUrl).then(function(data){
        var html = iconv.decode(data, 'utf-8')
        const $ = cheerio.load(html)
        var imgUrls= $('.thumb-container-big > .thumb-container > div > a >img').map(function(){
            var url = $(this).attr('data-src')
            var alt = $(this).attr('alt').split('ID:')
            var desc = alt[0]
            var id = alt[1]
            var data = {}
            data.id = id
            data.url = url
            data.desc = desc
            data.fileName = url.split('-').pop()
            imgData.push(data)
        })
        creatImg(imgData)
    })

    //创建文件
    function creatImg(data){
        var dist = path.resolve(process.cwd(), './dist')
        if (!fs.existsSync(dist)){
            fs.mkdirSync(dist)
        }
        console.log(data.length)
        data.map(function(item){
            var imgFileName = path.resolve(dist, path.basename(item.fileName))
            var data = {}
            data.id = item.id
            data.path = `../../dist/${item.fileName}`
            data.desc = item.desc
            imgJsonData.push(data)
            // 这里一次请求了30条，肯定有问题
            download(item.url).then(function(buffer){
                console.log(imgFileName)
                fs.writeFileSync(imgFileName, buffer)
            })
        })
        var imgListInfo = path.resolve(dist, './imgListInfo.json')
        fs.writeFileSync(imgListInfo, JSON.stringify(imgJsonData, null, 2))
    }
}